Repository navigation
fix(indexer): stop a reorg re-arming the whole init-gap heal ledger - #137
Merged
Merged
Conversation
Cap heal sweeps at the contract's first-seen block, scope the rollback re-arm to targets whose gap window reaches above the rollback point, and bound the drain per poll cycle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What happened
On staging, 2026-09-30 07:41–08:41, the V2 indexer followed the chain head for
9.4% of the hour. A single 1-block Sepolia reorg cost ~54 minutes of blackout:
07:47:36.949 REORG DETECTED at block 11813277 → rolled back to 11813276
07:47:42.211 init-gap heal: 1726 pending resolver_init_gaps target(s) ← 5.3s later
08:24:47.516 Discovery backfill: 1724 swept in 18 group(s), 0 anchors cached,
29852 events, 725 RPC calls, 2225305ms
08:24:47.608 init-gap heal: 680 pending subregistry_init_gaps target(s)
...still running 16 minutes later at the end of the log
No
Indexing v2 from blockline appears for the whole window, and the reorgcursor stays disarmed throughout — so a reorg arriving during it would write
uncaptured.
Root cause
Three compounding defects, all pre-existing (they arrived with the heal ledger,
not with any recent merge):
rollbackToBlockre-armed every target unconditionally. One blanketUPDATE completed = 0re-opened all 2,406 rows. Yet the 1,724 resolveranchors span 11708980–11812555 — every one predates the rollback point by
≥722 blocks and was provably untouched by the created-after deletes.
deadline, no resume; it drains both tasks to completion before the loop
continues.
init gap is only the events emitted before the indexer knew a contract
existed; everything after was already indexed. So ~25,000 of the 29,852
dispatched events were never owed, and 958,140 block-ranges were scanned for
104,298 distinct blocks — 9.2× redundant.
Where the 2,225,305 ms went
eth_getCoderequests at ~160 msgetLogs; the rest is PostgresThe sweep is 29,852 events × 70.2 ms of per-event unbatched autocommit. The
27.3 ms/statement figure is derived independently, from a different part of the
same log (a 65,710 ms boot gap ÷ 2,411 statements), so the accounting isn't
circular.
Worth flagging for anyone reading the raw log: the headline "725 calls in
2,225,305 ms = 3.07 s per call" is an artifact. 708 of those calls are the
anchor bisection; per-call latency was never the problem.
What this changes
Sweeps stop at the contract's first-seen block. Not a new convention — the
discovery-time path already capped itself this way; the heal path now matches its
sibling. Two safety properties, both tested:
Reading it as a literal ceiling would sweep nothing and leave a permanent gap.
LEFT JOINscoped by protocol. An inner join silently dropsan unregistered target out of the sweep; an address-only join lets a v1
sighting cap a v2 sweep (
known_*is keyed(protocol, address)).The rollback re-arm is scoped to targets whose gap window reaches above the
rollback point. A group sweeps to its highest member's ceiling, so a
lower-ceiling member does see events above its own — those are safe
specifically because they sit at or above that contract's first-seen block, so
the ordinary path indexed them too, and the sync-state rewind immediately after
this call re-creates them. An unknown window re-arms;
attemptsstill resets onevery row.
For the incident's exact shape, this is 0 re-arms instead of 2,406.
The drain is bounded by both of its cost drivers. A target cap for the anchor
bisection (which runs over every target before the first group sweeps, so a
time budget alone cannot bound a pass) and a wall-clock deadline checked
between groups for the sweeping. The first group always runs, so a budget below
one group's cost degrades to slow progress rather than livelock. Resume needs no
new state: an unswept target simply stays owed, which is already how a failed
sweep resumes.
Verification
test-alltest-pgtest-storage-pgtest-integration-pgzig fmt --check src build.zigschema/postgresin sync12 new tests. Each was sabotage-checked — the fix reverted, the suite
confirmed to fail — including the
+1boundary, the unknown-row fail-safe, theLEFT→INNERjoin, the protocol scoping, and the non-binding cap.Measured on a tree also carrying an unrelated v2-attribution change set (disjoint
files); that set accounts for ~30 of the test count.
Not included, deliberately
acquire()guards only ~2 of ~12 outbound requestsper cycle — every call site is an inter-chunk spacer whose guard is false on a
single-chunk pass — and it meters HTTP batches, not sub-calls: the 725 counted
calls carried ~68,000 JSON-RPC sub-calls, so the probe phase ran ~530
eth_getCode/s (~10,000 CU/s) while the boot line said "25 req/s". That's thedirect cause of the 15 provider throttles in the log. Deciding what to meter
and whether to add adaptive backpressure is a redesign, not a fix.
block_timestamphints on the registration path (~30k RPC calls onthe next resync) — they touch a file with unrelated pending work.
POLL_INTERVALand the indexer'sRATE_LIMIT_PER_SECONDare compose literals,so an operator can't tune either without editing the file.